package com.example.testing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class LoginUser extends Activity implements OnClickListener {
	String URL = "http://smoothalicious.nl/webservice/user/login.php",
    		result = "result",
    		details = "details",
    		entryTest = "entryTest",
    		email, password;
    Boolean pressedOnce;
	
    UserManagement userManagement;
    Intent intent;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      	this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.login_user);
      	
		pressedOnce = false;
		
		findViewById(R.id.login).setOnClickListener(this);
		findViewById(R.id.register).setOnClickListener(this);
	}
	
	public void onClick(View view) {
		if(view.getId() == R.id.login) {
			email = ((EditText) findViewById(R.id.email)).getText().toString();
			password = ((EditText) findViewById(R.id.password)).getText().toString();
			
			userManagement = new UserManagement(this);
			userManagement.loginUser(email, password);
		} else if (view.getId() == R.id.register) {
			intent = new Intent(LoginUser.this, AddUser.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            LoginUser.this.startActivity(intent);
		}
	}
	
	@Override
	protected void onResume() {
	    super.onResume();

	    pressedOnce = false;
	}

	@Override
	public void onBackPressed() {
	    if (pressedOnce) {
	        super.onBackPressed();
	        
	        finish();
	    } else {
	    	pressedOnce = true;
		    Toast.makeText(this, "Druk nog eens op terug om de app af te sluiten", Toast.LENGTH_SHORT).show();	    	
	    }
	}
}